package com.tool.btn;

import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import come.tool.JDialog.TiShiUtil;
import org.apache.commons.lang.StringUtils;
import org.come.Frame.*;
import org.come.Jpanel.*;
import org.come.bean.LoginResult;
import org.come.bean.SearchSellLiangHaoResultBean;
import org.come.entity.SellLiangHaoBase;
import org.come.entity.SellLianghaoAuc;
import org.come.model.Eshop;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.FormsManagement;
import org.come.until.GsonUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

public class LiangHaoItemBtn extends MoBanBtn {

    private int caozuo;

    private AucJpanel aucJpanel;

    private XinJianJpanel xinJianJpanel;

    private SellLiangHaoBase lianghaoItem;

    private SellLianghaoAuc sellLianghaoAuc;

    private GetLiangHaoTwoJpanel getLiangHaoTwoJpanel;

    private PaintLiangHaoJpanel paintLiangHaoJpanel;

    private ContinueLiangHaoJpanel continueLiangHaoJpanel;

    public LiangHaoItemBtn(String iconpath, int type, SellLiangHaoBase liangHaoItem, int caozuo,String text) {
        super(iconpath, type);
        setText(text);
        // TODO Auto-generated constructor stub
        this.lianghaoItem = liangHaoItem;
        this.caozuo = caozuo;
    }

    public LiangHaoItemBtn(String iconpath, int type, SellLianghaoAuc sellLianghaoAuc, int caozuo,String text) {
        super(iconpath, type);
        setText(text);
        // TODO Auto-generated constructor stub
        this.sellLianghaoAuc = sellLianghaoAuc;
        this.caozuo = caozuo;
    }



    public LiangHaoItemBtn(String iconpath, int type, ContinueLiangHaoJpanel continueLiangHaoJpanel, int caozuo, String text) {
        super(iconpath, type);
        setText(text);
        this.caozuo = caozuo;
        this.continueLiangHaoJpanel = continueLiangHaoJpanel;
    }

    public LiangHaoItemBtn(String iconpath, int type, int caozuo, String text, Color[] colors) {
        super(iconpath, type,colors);
        setText(text);
        this.setFont(UIUtils.TXT_hyzjt18);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.caozuo = caozuo;
    }

    public LiangHaoItemBtn(String iconpath, int type, AucJpanel aucJpanel, int caozuo,String text) {
        super(iconpath, type);
        setText(text);
        // TODO Auto-generated constructor stub
        this.aucJpanel = aucJpanel;
        this.caozuo = caozuo;
    }

    public LiangHaoItemBtn(String iconpath, int type, PaintLiangHaoJpanel paintLiangHaoJpanel, int caozuo,String text) {
        super(iconpath, type);
        setText(text);
        this.setFont(UIUtils.TEXT_FONT);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.paintLiangHaoJpanel = paintLiangHaoJpanel;
        this.caozuo = caozuo;
    }

    public LiangHaoItemBtn(String iconpath, int type, XinJianJpanel xinJianJpanel, int caozuo,String text) {
        super(iconpath, type);
        setText(text);
        this.xinJianJpanel = xinJianJpanel;
        this.caozuo = caozuo;
    }

    public LiangHaoItemBtn(String iconpath, int type, String text,int caozuo, GetLiangHaoTwoJpanel getLiangHaoTwoJpanel) {
        super(iconpath, type);
        setText(text);
        // TODO Auto-generated constructor stub
        this.getLiangHaoTwoJpanel = getLiangHaoTwoJpanel;
        this.caozuo = caozuo;
    }


    @Override
    public void chooseyes() {
    }

    @Override
    public void chooseno() {
    }

    @Override
    public void nochoose(MouseEvent e) {
        try {
            String mes = null;
            switch (caozuo) {
                case 1:
                    final LoginResult login = RoleData.getRoleData().getLoginResult();
                    if(StringUtils.isNotEmpty(login.getLiangHao())){
                        OptionsJframe.getOptionsJframe().getOptionsJpanel().showBox(TiShiUtil.buylianghao, this.lianghaoItem, "#W您确定购买靓号:#G" +this.lianghaoItem.getLianghao() + " #W吗?  #原靓号:#G"+login.getLiangHao()+"#W将会被覆盖。");
                    }else{
                        OptionsJframe.getOptionsJframe().getOptionsJpanel().showBox(TiShiUtil.buylianghao, this.lianghaoItem, "#W您确定购买靓号:#G" +this.lianghaoItem.getLianghao() + " #W吗?  #原靓号将会被覆盖。");
                    }

//                    mes = Agreement.getAgreement().selllianghaoAgreement("BUYLIANGHAO|" + GsonUtil.getGsonUtil().getgson().toJson(this.lianghaoItem));
//                    SendMessageUntil.toServer(mes);
                    break;
                case 2:

                    LocalDate expDate = LocalDate.parse(lianghaoItem.getAucEndTime());
                    LocalDate nowDate = LocalDate.now();
                    long daysBetween = ChronoUnit.DAYS.between(nowDate,expDate);
                    if (daysBetween<=0){
                        ZhuFrame.getZhuJpanel().addPrompt2("还未到竞拍时间！");
                        return;
                    }
                    FormsManagement.showForm(703);
                    AucJfram.getAucJfram().getAucjpanel().setReRuc(false);
                    AucJfram.getAucJfram().getAucjpanel().setLiangHaoItem(this.lianghaoItem);
//					String mes = Agreement.getAgreement().selllianghaoAgreement("BUYLIANGHAO|" + GsonUtil.getGsonUtil().getgson().toJson(this.lianghaoItem));
//					SendMessageUntil.toServer(mes);
                    break;
                case 3:
                    String aucPrice = this.aucJpanel.getAucPrice().getText();
                    if (!aucPrice.matches("\\d+")) {
                        ZhuFrame.getZhuJpanel().addPrompt2("请输入竞拍价格！");
                        return;
                    }
                    int aucPriceInt = Integer.parseInt(aucPrice);
                    SellLiangHaoBase oldSellLiangHaoBase = this.aucJpanel.getLiangHaoItem();
                    if(!this.aucJpanel.isReRuc() && oldSellLiangHaoBase!=null){
                        int oldPrice = oldSellLiangHaoBase.getPrice();
                        if (aucPriceInt <= oldPrice) {
                            ZhuFrame.getZhuJpanel().addPrompt2("出价不能低于当前价格");
                        } else {
                            SellLiangHaoBase newSellLiangHaoBase = new SellLiangHaoBase();
                            newSellLiangHaoBase.setAucPrice(aucPriceInt);
                            newSellLiangHaoBase.setType(oldSellLiangHaoBase.getType());
                            newSellLiangHaoBase.setLianghao(oldSellLiangHaoBase.getLianghao());
                            newSellLiangHaoBase.setPrice(oldSellLiangHaoBase.getPrice());
                            newSellLiangHaoBase.setAucEndTime(oldSellLiangHaoBase.getAucEndTime());
                            newSellLiangHaoBase.setAucStartTime(oldSellLiangHaoBase.getAucStartTime());
                            mes = Agreement.getAgreement().selllianghaoAgreement("BUYAUC|" + GsonUtil.getGsonUtil().getgson().toJson(newSellLiangHaoBase));
                            SendMessageUntil.toServer(mes);
                        }
                    }else if(this.getAucJpanel().getSelllianghaoAuc()!=null){
                        int oldPrice =  this.getAucJpanel().getSelllianghaoAuc().getAucPoint().intValue();
                        if (aucPriceInt <= oldPrice) {
                            ZhuFrame.getZhuJpanel().addPrompt2("出价不能低于当前价格");
                        } else {
                            List<SellLiangHaoBase> list = GetLiangHaoJframe.getGetLiangHaoJframe().getGetLiangHaoJpanel().getGetliangHaoTabJpanel().getGetLiangHaoTwoJpanel().getnLhList();
                            if(list!=null && list.size()>0){
                                for(SellLiangHaoBase item:list){
                                    if(item.getLianghao().equals(this.getAucJpanel().getSelllianghaoAuc().getLianghao())){

                                        SellLiangHaoBase newSellLiangHaoBase2 = new SellLiangHaoBase();
                                        newSellLiangHaoBase2.setAucPrice(aucPriceInt);
                                        newSellLiangHaoBase2.setType(item.getType());
                                        newSellLiangHaoBase2.setLianghao(item.getLianghao());
                                        newSellLiangHaoBase2.setPrice(item.getPrice());
                                        newSellLiangHaoBase2.setAucEndTime(item.getAucEndTime());
                                        newSellLiangHaoBase2.setAucStartTime(item.getAucStartTime());

                                        mes = Agreement.getAgreement().selllianghaoAgreement("BUYAUC|" + GsonUtil.getGsonUtil().getgson().toJson(newSellLiangHaoBase2));
                                        FormsManagement.HideForm(703);
                                        SendMessageUntil.toServer(mes);
                                        return;
                                    }
                                }
                            }
                        }
                    }
                    FormsManagement.HideForm(703);
                    break;
                case 4:         //上一页
                    if(!getLiangHaoTwoJpanel.isMylist()){
                        SearchSellLiangHaoResultBean lhBean = getLiangHaoTwoJpanel.getAlb();
                        if (lhBean == null || lhBean.getSellLiangHaos() == null || lhBean.getSellLiangHaos().size() <= 0)
                            return;
                        getLiangHaoTwoJpanel.setNowpage(getLiangHaoTwoJpanel.getNowpage() - 1 <= 1 ? 1
                                : getLiangHaoTwoJpanel.getNowpage() - 1);
                        int totalpage = lhBean.getSellLiangHaos().size() / 6 + (lhBean.getSellLiangHaos().size() % 6 == 0 ? 0 : 1);
                        // 刷新界面
                        getLiangHaoTwoJpanel.refreshSellList(lhBean.getSellLiangHaos(), getLiangHaoTwoJpanel.getNowpage());
                    }else{
                        List<SellLianghaoAuc> aucList = getLiangHaoTwoJpanel.getMyLiangHaoAucBean().getSellLianghaoAucs();
                        if ( aucList == null ||aucList.size() <= 0)
                            return;
                        getLiangHaoTwoJpanel.setMy_nowpage(getLiangHaoTwoJpanel.getMy_nowpage() - 1 <= 1 ? 1
                                : getLiangHaoTwoJpanel.getMy_nowpage() - 1);
                        int totalpage = aucList.size() / 8 + (aucList.size() % 8 == 0 ? 0 : 1);
                        // 刷新界面
                        getLiangHaoTwoJpanel.refreshMyAucList(aucList, getLiangHaoTwoJpanel.getMy_nowpage());
                    }
                    break;
                case 5:         //下一页
                    if(!getLiangHaoTwoJpanel.isMylist()) {
                        SearchSellLiangHaoResultBean lhBean = getLiangHaoTwoJpanel.getAlb();
                        if (lhBean == null || lhBean.getSellLiangHaos() == null || lhBean.getSellLiangHaos().size() <= 0)
                            return;
                        // 总页数
                        int totalpage = lhBean.getSellLiangHaos().size() / 6 + (lhBean.getSellLiangHaos().size() % 6 == 0 ? 0 : 1);
                        getLiangHaoTwoJpanel.setNowpage(getLiangHaoTwoJpanel.getNowpage() + 1 >= totalpage ? totalpage
                                : getLiangHaoTwoJpanel.getNowpage() + 1);
                        // 刷新界面
                        getLiangHaoTwoJpanel.refreshSellList(lhBean.getSellLiangHaos(), getLiangHaoTwoJpanel.getNowpage());
                    }else{
//                        List<SellLianghaoAuc> aucList = getLiangHaoTwoJpanel.getMyLiangHaoAucBean().getSellLianghaoAucs();
//                        if ( aucList == null ||aucList.size() <= 0)
//                            return;
//                        getLiangHaoTwoJpanel.setMy_nowpage(getLiangHaoTwoJpanel.getMy_nowpage() - 1 <= 1 ? 1
//                                : getLiangHaoTwoJpanel.getMy_nowpage() - 1);
//                        int totalpage = aucList.size() / 8 + (aucList.size() % 8 == 0 ? 0 : 1);
//                        // 刷新界面
//                        getLiangHaoTwoJpanel.refreshMyAucList(aucList, getLiangHaoTwoJpanel.getMy_nowpage());


                        List<SellLianghaoAuc> aucList = getLiangHaoTwoJpanel.getMyLiangHaoAucBean().getSellLianghaoAucs();
                        if ( aucList == null || aucList.size() <= 0)
                            return;
                        // 总页数
                        int totalpage = aucList.size() / 8 + (aucList.size() % 8 == 0 ? 0 : 1);
                        getLiangHaoTwoJpanel.setMy_nowpage(getLiangHaoTwoJpanel.getMy_nowpage() + 1 >= totalpage ? totalpage
                                : getLiangHaoTwoJpanel.getMy_nowpage() + 1);
                        // 刷新界面
                        getLiangHaoTwoJpanel.refreshMyAucList(aucList, getLiangHaoTwoJpanel.getMy_nowpage());
                    }
                    break;
                case 6:

                    String xjlianghao = this.xinJianJpanel.getxNum().getText();
                    SellLianghaoAuc selllianghaoauc = new SellLianghaoAuc();
                    selllianghaoauc.setLianghao(xjlianghao);
                    selllianghaoauc.setOriginalprice(198);//靓号信笺购买靓号需要的积分
                    if(xjlianghao==null ||xjlianghao.length()<4 || xjlianghao.length()>6){
                        ZhuFrame.getZhuJpanel().addPrompt2("请输入4到6位靓号！");
                        return;
                    }
                    mes = Agreement.getAgreement().selllianghaoAgreement("XJLIANGHAO|" + GsonUtil.getGsonUtil().getgson().toJson(selllianghaoauc));
                    SendMessageUntil.toServer(mes);
                    FormsManagement.HideForm(704);
                    break;
                case 7:         //预览靓号

                    GetLiangHaoJframe.getGetLiangHaoJframe().getGetLiangHaoJpanel().getGetliangHaoTabJpanel().getGetLiangHaoTwoJpanel().setPreViewLiangHao(this.lianghaoItem.getLianghao());
                    FormsManagement.showForm(707);
                    break;
                case 8:         //竞拍操作
                    if(sellLianghaoAuc!=null ){
                        int status = sellLianghaoAuc.getStatus();
                        if(status == 1){    //放弃
                            mes = Agreement.getAgreement().selllianghaoAgreement("DELAUC|" + GsonUtil.getGsonUtil().getgson().toJson(sellLianghaoAuc));
                            SendMessageUntil.toServer(mes);
                        }else if(status == 2){

                        }else if(status == 3 || status == 4 || status == 5 ){
                            AucJfram.getAucJfram().getAucjpanel().setSelllianghaoAuc(this.sellLianghaoAuc);
                            AucJfram.getAucJfram().getAucjpanel().setReRuc(true);
                            FormsManagement.showForm(703);
                            //AucJfram.getAucJfram().getAucjpanel().setLiangHaoItem(this.lianghaoItem);
                        }
                    }

                    break;
                case 9:
                    FormsManagement.HideForm(704);
                    break;
                case 10:
                    FormsManagement.HideForm(703);
                    break;
                case 50:        //更改配色方案
                    this.paintLiangHaoJpanel.setSelectType(1);
                    break;
                case 51:        //更改配色方案
                    this.paintLiangHaoJpanel.setSelectType(2);
                    break;
                case 52:        //更改配色方案
                    this.paintLiangHaoJpanel.setSelectType(3);
                    break;
                case 53:        //更改配色方案
                    this.paintLiangHaoJpanel.setSelectType(4);
                    break;
                case 70:        //确定续费
                    String mes2 = Agreement.getAgreement().selllianghaoAgreement("ADDLHTIME|"+1);
                    SendMessageUntil.toServer(mes2);
                    break;
                case 71:        //取消续费
                    FormsManagement.HideForm(706);
                    break;
            }
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

    public int getCaozuo() {
        return caozuo;
    }

    public void setCaozuo(int caozuo) {
        this.caozuo = caozuo;
    }

    public AucJpanel getAucJpanel() {
        return aucJpanel;
    }

    public void setAucJpanel(AucJpanel aucJpanel) {
        this.aucJpanel = aucJpanel;
    }

    public SellLiangHaoBase getLianghaoItem() {
        return lianghaoItem;
    }

    public void setLianghaoItem(SellLiangHaoBase lianghaoItem) {
        this.lianghaoItem = lianghaoItem;
    }

    public SellLianghaoAuc getSellLianghaoAuc() {
        return sellLianghaoAuc;
    }

    public void setSellLianghaoAuc(SellLianghaoAuc sellLianghaoAuc) {
        this.sellLianghaoAuc = sellLianghaoAuc;
    }
}
